package com.itheima.Controller;

import com.itheima.Service.DeptService;
import com.itheima.Service.impl.DeptServiceImpl;
import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/depts")
@RestController
@Slf4j

public class DeptController {
    //记录日志对象


    @Autowired
    private DeptService deptService;


    @GetMapping
    public Result list() throws Exception {
        log.info("查询所有部门信息");
        List<Dept> deptList = deptService.queryDeptList();
        return Result.success(deptList);
    }

    //    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idstr = request.getParameter("id");
//        int id = Integer.parseInt(idstr);
//        System.out.println("id:"+id);
//        return Result.success();
//    }
    @Log
    @DeleteMapping
    public Result delete(Integer id) {
        log.info("删除部门:{}", id);
        System.out.println("id:" + id);
        deptService.delete(id);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result save(@RequestBody Dept dept) {
        log.info("新增部门:{}", dept);
        System.out.println("新增部门:" + dept);
        deptService.save(dept);
        return Result.success();
    }

    @Log
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询部门id:{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门信息:{}", dept);
        deptService.update(dept);
        return Result.success();
    }
}
